package com.windchime.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.windchime.blog.entity.Comment;
import com.windchime.blog.result.R;
import com.windchime.blog.service.CommentService;
import com.windchime.blog.utils.StringSplitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author WindChime
 * @since 2021-08-05
 */
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 分页查询
     * @param page 当前页
     * @param limit 当前页条目数量
     * @return commentLists
     */
    @GetMapping("/selectCommentList")
    public R selectCommentList(Integer page, Integer limit) {
        PageInfo<Comment> commentLists = commentService.selectCommentList(page, limit);
        return R.ok().code(0).msg("").count(commentLists.getTotal()).data("comments", commentLists.getList());
    }

    /**
     * 回复评论
     * @param comment 评论
     * @return true
     */
    @PostMapping("/replyComment")
    public R replyComment(Comment comment) {
        boolean b = commentService.replyComment(comment);
        if (b) {
            return R.ok().msg("回复成功");
        } else {
            return R.error().msg("回复失败");
        }
    }

    /**
     * 管理员发表评论
     * @param comment 评论
     * @return true
     */
    @PostMapping("/saveComment")
    public R saveComment(Comment comment) {
        boolean b = commentService.replyComment(comment);
        if (b) {
            return R.ok().msg("发表成功");
        } else {
            return R.error().msg("发表失败");
        }
    }

    /**
     * 删除评论
     * @param id 评论ID
     * @return true
     */
    @GetMapping("/deleteComment/{id}")
    public R deleteComment(@PathVariable Long id) {
        boolean b = commentService.removeById(id);
        if (b) {
            return R.ok().msg("删除成功");
        } else {
            return R.error().msg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids 评论ID字符串数组
     * @return true
     */
    @GetMapping("/deleteComments/{ids}")
    public R deleteComment(@PathVariable String ids) {
        List<Long> idList = StringSplitUtil.split(ids, ",");
        boolean b = commentService.removeByIds(idList);
        if (b) {
            return R.ok().msg("批量删除成功");
        } else {
            return R.error().msg("批量删除失败");
        }
    }

}

